package com.lych.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.lych.dubbo.utils.IdWorker;
import com.lych.dubbo.utils.TimeLineService;
import com.lych.model.enums.CommentType;
import com.lych.model.mongo.Comment;
import com.lych.model.mongo.Movement;
import com.lych.model.mongo.MovementTimeline;
import com.lych.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class MovementApiImpl implements MovementApi {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private TimeLineService timeLineService;

    /**
     * 动态-发布
     * @param movement
     */
    @Override
    public Movement movements(Movement movement) {
        //保存动态详细信息
        //设置PID
        movement.setPid(idWorker.getNextId("movement"));
        //设置时间
        movement.setCreated(System.currentTimeMillis());
        //保存动态数据
        Movement movement1 = mongoTemplate.save(movement);
        //调用方法保存好友数据与时间线数据
        timeLineService.saveTimeLine(movement.getUserId(),movement.getId());

        return movement1;
    }

    /**
     * 我的动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult all(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query created = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Movement> movements = mongoTemplate.find(created, Movement.class);
        return new PageResult(page,pagesize,0,movements);

    }

    /**
     * 查询好友动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Movement> findFriendMovements(Long userId, Integer page, Integer pagesize) {
        //查询好友时间线表
        Criteria criteria = Criteria.where("friendId").in(userId);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<MovementTimeline> movementTimelines = mongoTemplate.find(query, MovementTimeline.class);
        //提取动态id集合
        List<ObjectId> ids = CollUtil.getFieldValues(movementTimelines, "movementId", ObjectId.class);
        //通过动态id查询详细动态信息
        Query movements = Query.query(Criteria.where("id").in(ids));
        List<Movement> list = mongoTemplate.find(movements, Movement.class);
        //返回详细动态信息
        return list;
    }

    /**
     * 随机获取动态详细信息
     * @param counts
     * @return
     */
    @Override
    public List<Movement> randomMovements(Integer counts) {
        TypedAggregation aggregation = Aggregation.newAggregation(Movement.class,
                Aggregation.sample(counts));

        AggregationResults<Movement> movements = mongoTemplate.aggregate(aggregation,Movement.class);

        return movements.getMappedResults();
    }

    /**
     * 根据pid查询对应动态信息
     * @param pids
     * @return
     */
    @Override
    public List<Movement> findByPids(List<Long> pids) {
        Query query = Query.query(Criteria.where("pid").in(pids));
        List<Movement> list = mongoTemplate.find(query, Movement.class);
        return list;
    }

    /**
     * 根据id查询动态
     * @param movementId
     * @return
     */
    @Override
    public Movement findById(String movementId) {
        return mongoTemplate.findById(movementId,Movement.class);
    }

    /**
     * 根据id查询动态
     * @param page
     * @param pagesize
     * @param uid
     * @param state
     * @return
     */
    @Override
    public List<Movement> findByUserId(Integer page, Integer pagesize, Long uid, Integer state) {
        Query query = new Query();
        if (uid != null) {
            query.addCriteria(Criteria.where("userId").is(uid));
        }
        if (state != null) {
            query.addCriteria(Criteria.where("state").is(state));
        }
        query.skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("create")));
        return mongoTemplate.find(query,Movement.class);
    }

}
